home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / exec.swg / 0017_Self-Modifying EXE Files.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-01-27  |  2.7 KB  |  66 lines

  1. {
  2. OK. Maybe this isn't exactly what you were asking for, but I've seen quite a
  3. number of variations on this peeka-boo-into-the-exe-file, so I felt I just had
  4. to write a comment to this matter.
  5.  
  6.    Using some kind of a magic constant, which is then searched for in the exe
  7. file, probably is the most common approach to this kind of problem. But there's
  8. really no need to do a search. You can calculate exactly where any const is (or
  9. should be) located.
  10.  
  11.    The trick is to use a couple of simple facts:
  12.  
  13.    1/ The size of the exe header, in paragraphs, is located at byte 8 in the
  14. header (actually it's a word made up by bytes 8 and 9 but I still haven't seen
  15. an exe header of more than 4k, so I make it simple for myself using only the
  16. byte).
  17.  
  18.    2/ After the exe header comes the code segment and then directly the data
  19. segment. Thus the size of the code segment can be calculated by a simple dseg-
  20. cseg. Still talking paragraphs.
  21.  
  22.    3/ Now we've reached the data segment in the exe file. The location in the
  23. data segment can be found with ofs. Here we're talking bytes.
  24.  
  25.    Using these facts, here's a simple sample that let's you change a const
  26. string to whatever paramstr(1) you supply. Hope you'll be able to pick out the
  27. stuff you may find any need for.
  28.  
  29.    Since this code was extracted from a pretty small program I once wrote, it
  30. uses the rather crude method to read the entire exe file into a buffer, and
  31. then creating a new file blockwriting the entire buffer. If your program is
  32. larger than 64k you obviously need to use some other method.
  33. }
  34.  
  35. program SelfModifier;   (* Looks for a const and alters it *)
  36.                         (* Puts paramstr(1) into Name *)
  37.  
  38. const
  39.     Name : string = 'Fix me up';      {get 256 bytes to play with}
  40. type
  41.     Buffer = array[0..$3fff] of byte;
  42. var
  43.     ExeFile : file;
  44.     P       : ^Buffer;
  45.     N,I,O   : word;
  46.     NStr    : string;
  47.  
  48. begin
  49.  begin
  50.   new(P);                             {get mem for our buffer}
  51.   assign(ExeFile,paramstr(0));        {get myself}
  52.   reset(ExeFile,1);
  53.   blockread(ExeFile,P^,sizeof(Buffer),N);
  54.   close(ExeFile);                     {got it into Buf, now close it}
  55.   O:=(dseg-cseg+word(P^[8])) shl 4;   {start of data seg in exe file}
  56.   writeln('Name: ',Name);
  57.   NStr := paramstr(1);                {new string to put in Name}
  58.   inc(O,ofs(Name));                   {where Name is located}
  59.   move(NStr[0],P^[O],length(NStr)+1); {move string incl. length byte}
  60.   rewrite(ExeFile,1);                 {create new version}
  61.   blockwrite(ExeFile,P^,N);           {write it}
  62.   close(ExeFile);                     {close it...}
  63.   dispose(P)                          {...and release mem}
  64.  end
  65. end.
  66.